/*
 * Block comments with details of changes
 */
public class ProjectManangeDetailHandler
{
    /*
    ///新建的时候计算预计完成时间
    public static void estimateDate(List<ProjectManageDetail__c>list_new ){
    	for(ProjectManageDetail__c p:list_new ){
            if(p.StartTime__c != null && p.estimateTime__c !=null && p.Status__c !='完成'){
            	p.EstimateDate__c = p.StartTime__c.addDays(Integer.ValueOf(p.estimateTime__c));
            }
        }
    }
    ///更新
    public void calculateAcomplementDate(List<ProjectManageDetail__c>list_new )
    {
        Map<Id,Id>projectMap = new Map<Id,Id>();
        Map<Id,List<ProjectManageDetail__c>> pmdMap = new Map<Id,List<ProjectManageDetail__c>>();
      
        ///更新的子任务
        Set<Id> detailsSet = new Set<Id>();
        ///查出所有相关的项目
        for(ProjectManageDetail__c p:list_new ){
            projectMap.put(p.Project__c,p.Project__c);
            detailsSet.add(p.Id);
        }
        
        //查出所有的项目
        Map<Id,DevelopProject__c> dpMap = new Map<Id,DevelopProject__c>();
        for(DevelopProject__c dp :[select Id,EstimateDate__c from DevelopProject__c where Id in:projectMap.keySet()]){
            dpMap.put(dp.Id,dp);
        }
        //查出所有的子任务
        for(Id projectId  :projectMap.keySet()){
            List<ProjectManageDetail__c> pmdList = new List<ProjectManageDetail__c>();
            for(ProjectManageDetail__c pm: [select Id,Name,StartTime__c,EndTime__c,Status__c,FatherTaskDetail__c,Owner__c,Project__c,
                       estimateTime__c from ProjectManageDetail__c where Project__c in:projectMap.keySet() order by Status__c desc]){
					if(pm.Project__c == projectId){
                    	pmdList.add(pm);
                    }
            }
            pmdMap.put(projectId,pmdList);
        }
        

        //计算出所有项目的结构
        Map<Id,List<ProjectManageDetailBean>> projectDetails = new Map<Id,List<ProjectManageDetailBean>>();
        List<DevelopProject__c>dpList = new List<DevelopProject__c>();
        for(Id projectId  :projectMap.keySet()){
            ///计算出id=projectId 项目结构
            Map<Id,List<ProjectManageDetailBean>> temp  = calculateStuct(pmdMap.get(projectId),null);
            Map<Id,Date>dateMap = new Map<Id,date>();
            ///计算出不同所有者的最大完成时间
            for(Id owner :temp.keySet()){
                Date acompeleteDate= null;
                for(ProjectManageDetailBean pb: temp.get(owner)){
                    Date tempDate =  calculateDate(pb);
                    acompeleteDate = acompeleteDate == null ?  tempDate: acompeleteDate > tempDate ? acompeleteDate :tempDate; 
                }
                dateMap.put(owner,acompeleteDate);
            }
            Date acompeleteDate = null;
            for(Date d :dateMap.values()){
                acompeleteDate = acompeleteDate == null ?  d: acompeleteDate > d ? acompeleteDate :d; 
            }
            //保存最大完成时间
            if(acompeleteDate != null &&  dpMap.containsKey(projectId)){
                DevelopProject__c dp = dpMap.get(projectId);
                System.debug('最大完成时间:'+acompeleteDate);
                dp.EstimateDate__c = acompeleteDate ;
                dpList.add(dp);
            }
        }
        if(dpList.size()>0)update dpList; 
    }
    ///迭代 计算项目的结构
    public  Map<Id,List<ProjectManageDetailBean>>  calculateStuct(List<ProjectManageDetail__c> pmdList,Id fatherId){
        Map<Id,List<ProjectManageDetailBean>> temp = new Map<Id,List<ProjectManageDetailBean>>();
        List<ProjectManageDetail__c> te = new List<ProjectManageDetail__c>();
        Set<Id> ownerset = new Set<Id>();
        //找出当层的所有任务
        for(ProjectManageDetail__c p:pmdList){
            if(p.FatherTaskDetail__c == fatherId){
                te.add(p);
                ownerset.add(p.Owner__c);
            }
        }
        ///重新分组
        for(Id owner :ownerset){
            List<ProjectManageDetailBean> pbList = new List<ProjectManageDetailBean>();
            for(ProjectManageDetail__c p : te){
                if(p.Owner__c == owner){
                    ProjectManageDetailBean pb = new ProjectManageDetailBean();
                    pb.p = p ;
                    pb.childs = calculateStuct(pmdList,p.Id);
                    
                    pbList.add(pb);
                }
            }
            temp.put(owner,pbList);
        }

        System.debug('项目结构:'+temp);
        return temp;
    }

    ////迭代 计算项目的完成时间
    public Date calculateDate(ProjectManageDetailBean pb){

        if(pb.childs !=null && pb.childs.keySet().size() >0){///如果有子任务 
            ///计算子任务的预计完成时间
            ///计算本层 属于不同人的任务的最大完成时间
            Map<Id,Date> peopleDateMap = new Map<Id,Date>();
            
            for(Id owner :pb.childs.keySet()){
            	Date maxDate = null;
                for(ProjectManageDetailBean p:pb.childs.get(owner)){
                    ProjectManageDetail__c pmd = p.p;
                   p.estimateEndDate = calculateDate(p);
                    maxDate = maxDate == null ? p.estimateEndDate : maxDate > p.estimateEndDate ? maxDate :p.estimateEndDate;
                    System.debug('11111'+pmd.name +':'+maxDate);
                } 
                peopleDateMap.put(owner,maxDate);
            }

            Date maxDate1 =null;
            System.debug(peopleDateMap);
            for(Date d :peopleDateMap.values()){
                maxDate1 = maxDate1 == null ? d : maxDate1 > d ? maxDate1 :d;
            }
            System.debug('本层最大时间'+maxDate1);
            return maxDate1;

        }else{///如果没有子任务
            if(pb.p.Status__c == '完成'){ ///已经完成 结束时间即为 预计完成时间
               // pb.estimateEndDate = pb.p.EndTime__c;
                return pb.p.EndTime__c;
            }else if(pb.p.Status__c  == '正在进行'){ //正在进行  开始时间 +预计耗时 为预计完成时间
                //pb.estimateEndDate = pb.p.StartTime__c.addDays( Integer.ValueOf(pb.p.estimateTime__c));
                return  pb.p.StartTime__c.addDays( Integer.ValueOf(pb.p.estimateTime__c));
                //return dateAdd(Integer.ValueOf(pb.p.estimateTime__c),pb.p.StartTime__c);
            }else{///未开始  
                // pb.p.EndTime__c = pb.p.StartTime__c.addDays (Integer.ValueOf(pb.p.estimateTime__c));
               // pb.estimateEndDate = pb.p.StartTime__c.addDays (Integer.ValueOf(pb.p.estimateTime__c));
                return pb.p.StartTime__c.addDays (Integer.ValueOf(pb.p.estimateTime__c));
                // return dateAdd(Integer.ValueOf(pb.p.estimateTime__c),startTime);
            }

        } 

        return null;
    }
    ///排除周末;
    public Date dateAdd(Integer n,Date d){
        if(d.toStartOfWeek().daysBetween(d)> 4)d = d.addDays(d.toStartOfWeek().daysBetween(d));
        for(Integer i = 0 ;i < n; i++){
            d = d.addDays(1);
            if(d.toStartOfWeek().daysBetween(d)> 4)d = d.addDays(d.toStartOfWeek().daysBetween(d));
        }
        if(d.toStartOfWeek().daysBetween(d)> 4)d = d.addDays(d.toStartOfWeek().daysBetween(d));
        return d;
    }
    public class ProjectManageDetailBean{
        public  ProjectManageDetail__c p;
        public Map<Id,List<ProjectManageDetailBean>> childs;
        public Date estimateEndDate ;///预计完成时间;
        public Date estimateStartDate;//预计开始时间;
    }
    */

}